//待预约 退款关闭订单
<template>
  <div class="addPrice">
    <el-dialog title="确认退款关闭订单" :visible.sync="visible" width="40%" modal-append-to-body center>
      <el-form
        v-if="visible"
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="110px"
        label-position="left"
      >
        <p>
          订单下单实付金额：{{
          orderInfo && orderInfo.payAmount | filterPriceFormat
          }}元
        </p>
        <p>累计加价：{{ payAmountList / 100 }}元</p>
        <el-form-item label="退款金额" prop="saleAmount">
          <el-input-number
            :controls="false"
            :precision="2"
            :step="1"
            :min="0.01"
            :max="999"
            v-model="ruleForm.saleAmount"
          ></el-input-number>元
        </el-form-item>
        <el-form-item label="退款关闭原因" prop="refundReason">
          <el-select v-model="ruleForm.refundReason" placeholder="请选择">
            <el-option
              v-for="item in reasonList"
              :key="item.dictLabel"
              :label="item.dictLabel"
              :value="item.dictLabel"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="problem">
          <el-input type="textarea" v-model="ruleForm.problem"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="info" @click="back">返回</el-button>
        <el-button type="danger" @click="submit">退款</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { apiDictRefundCloseReason } from "@/api/order.js";
export default {
  components: {},
  props: {
    orderInfo: {
      type: Object,
      default: () => {}
    }
  },
  computed: {
    payAmountList() {
      const num1 =
        this.orderInfo &&
        "payAmountList" in this.orderInfo &&
        this.orderInfo.payAmountList
          ? this.orderInfo.payAmountList.reduce((prev, cur) => {
              return Number(prev) + Number(cur);
            }, 0)
          : 0;
     
      console.log(num1, );
      return num1 + 0 ;
    }
  },
  data() {
    const checkOrderPrice = (rule, value, callback) => {
      console.log(this.orderInfo.payAmount + this.payAmountList);
      if (
        value * 100 >
        Number(this.orderInfo.payAmount) + Number(this.payAmountList)
      ) {
        callback(new Error("退款金额不得大于实付金额与累计加价金额之和"));
      } else {
        callback();
      }
    };

    return {
      visible: false,
      ruleForm: {
        saleAmount: undefined,
        refundReason: "",
        problem: ""
      },
      reasonList: [],
      rules: {
        saleAmount: [
          { required: true, message: "请填写退款金额", trigger: "blur" },
          { validator: checkOrderPrice, trigger: "blur" }
        ],
        refundReason: [
          { required: true, message: "请选择加价原因", trigger: "blur" }
        ],
        problem: [{ required: true, message: "请填写描述", trigger: "blur" }]
      }
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      apiDictRefundCloseReason().then(res => {
        this.reasonList = res.data;
      });
    },
    onShow() {
      this.visible = true;
      this.ruleForm = {
        saleAmount: undefined,
        refundReason: "",
        problem: ""
      };
    },
    back() {
      this.$refs.ruleForm.resetFields();
      this.visible = false;
    },
    submit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          console.log(this, "退款");
          this.$emit("refundClose", this.ruleForm);
          this.visible = false;
          this.$nextTick(() => {
            this.$refs.ruleForm.resetFields();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

